package whf.framework.meta;

import java.util.List;

import whf.framework.cache.Cache;
import whf.framework.cache.CacheManagerImp;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.entity.IMeta;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.meta.service.ViewServiceImp;
import whf.framework.service.SpringService;

/**
 * @author wanghaifeng
 *
 */
public class MetaManager extends SpringService {
	private Cache<String, IMeta> __cache = null;
	
	private whf.framework.meta.service.ObjectService __objectService;
	
	private whf.framework.meta.service.ViewService __viewService;
	
	private static Log log = LogFactory.getLog(MetaManager.class);
	
	private static MetaManager instance = null;
	
	public static MetaManager getDefaultInstance(){
		if(instance == null){
			instance = new MetaManager();
		}
		return instance;
	}
	
	public Cache<String, IMeta> getCache(){
		if(this.__cache == null){
			this.__cache = CacheManagerImp.getCacheManager().getCache(MetaManager.class.getName(), false);
		}
		return this.__cache;
	}
	
	public void setCache(Cache<String, IMeta> cache){ 
		this.__cache = cache;
	}
	
	private whf.framework.meta.service.ObjectService getObjectService() throws ServiceNotFoundException{
		if(this.__objectService == null){
			this.__objectService = ObjectServiceImp.getObjectService();
		}
		return this.__objectService;
	}
	
	private whf.framework.meta.service.ViewService getViewService() throws ServiceNotFoundException{
		if(this.__viewService == null){
			this.__viewService = ViewServiceImp.getViewService();
		}
		return this.__viewService;
	}
	
	public final whf.framework.meta.entity.Object findByEntityClass(String boClassName){
		String key = whf.framework.meta.entity.Object.class.getName() + ":" + boClassName;
		whf.framework.meta.entity.Object obj = null;
		Cache<String, IMeta> cache = this.getCache();
		if(cache != null)
			obj = (whf.framework.meta.entity.Object)cache.get(key);
		if(obj == null) {
			try{
				obj = this.getObjectService().findByEntityClass(boClassName);
				if(cache != null)
					cache.put(key, obj);
			} catch(Exception e){
				log.error(e);
			}
		}
		return obj;
	}
	
	public final whf.framework.meta.entity.Object findByActionClass(String actionClassName){
		String key = whf.framework.meta.entity.Object.class.getName() + ":" + actionClassName;
		whf.framework.meta.entity.Object obj = null;
		Cache<String, IMeta> cache = this.getCache();
		if(cache != null)
			obj = (whf.framework.meta.entity.Object)cache.get(key);
		if(obj == null) {
			try{
				obj = this.getObjectService().findByActionClass(actionClassName);
				if(cache != null)
					cache.put(key, obj);
			} catch(Exception e){
				log.error(e);
			}
		}
		return obj;
	}
	
	public final whf.framework.meta.entity.View findView(String code) {
		String key = whf.framework.meta.entity.View.class.getName() + ":" + code;
		whf.framework.meta.entity.View view = null;
		Cache<String, IMeta> cache = this.getCache();
		if(cache != null)
			view = (whf.framework.meta.entity.View)cache.get(key);
		if(view == null){
			try{
				List list = this.getViewService().findWithoutLazy("t.code=?", code);
				if(list.size() > 0)
					view = (whf.framework.meta.entity.View)list.get(0);
				if(cache != null)
					cache.put(key, view);
			} catch(Exception e){
				log.error(e);
			}
		}
		return view;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		instance = this;
		super.afterPropertiesSet();
	} 
}
